<?php
$vurl = $_REQUEST['vod_url'];
$md5url = md5($vurl);
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="renderer" content="webkit">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dplayer@latest/dist/DPlayer.min.css?v=4">
<style type="text/css">
    body,html{width:100%;height:100%;background:#000;padding:0;margin:0;overflow-x:hidden;overflow-y:hidden}
    .dplayer, .dplayer-video-wrap{height:100%;}

    *{margin:0;border:0;padding:0;text-decoration:none}
    #stats{position:absolute;top:5px;left:6px;font-size:10px;color:#fdfdfd;z-index:2147;text-shadow:1px 1px 1px #000, 1px 1px 1px #000}
    #dplayer{position:inherit;}
    .txtwarn{color:#FF3300;padding-top: 30px;  font-size: 26px;line-height: 48px;}
</style>
</head>
<body>
<?php if($vurl!='null'){ ?>
<div id="dplayer"></div>
<div id="stats">p2p</div>
<?php }else{   ?>
    <div class="txtwarn">
        由于未知的原因该播放地址失效，请选择其他播放源<br>
        下方“来源”呈现<span style="color:#5cb85c">绿色</span>才是当前播放源，如果没有呈现<span style="color:#5cb85c">绿色</span>请点击选中播放源。
    </div>
<?php } ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-132431571-1"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-132431571-1');
</script>

<script src="https://cdn.jsdelivr.net/npm/cdnbye@latest?v=5"></script>
<script src="https://cdn.jsdelivr.net/npm/dplayer@latest?v=5"></script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js?v=2"></script>
<?php if($vurl!='null'){ ?>
<script>
    var vod_id = window.parent.cms_player?window.parent.cms_player.vod_id:null;
    var vod_name = window.parent.cms_player?window.parent.cms_player.vod_name:null;
    var vod_pic = window.parent.cms_player?window.parent.cms_player.vod_pic:null;
    var vod_cid = window.parent.cms_player?window.parent.cms_player.vod_cid:null;
    var play_sid = window.parent.cms_player?window.parent.cms_player.play_sid:null;
    var play_pid = window.parent.cms_player?window.parent.cms_player.play_pid:null;
    var play_time = 0;
    var start_play_time = 0;
    var m3u8url =  '<?php echo $vurl; ?>';

    var _peerId = '';
    var _peerNum = 0;
    var _totalP2PDownloaded = 0;
    var _totalP2PUploaded = 0;
    var _totalHTTPDownloaded = 0;

    // 是否已经执行了seek
    var hasSeeked = false;
    // 是否已经Play了
    var playStatus = false;

    // 当前影片的seekKey
    var seekKey = 'pay'+vod_id;
    if(vod_cid && vod_cid != 1 && play_pid!=null && vod_id !=null ){
        seekKey += "_"+play_pid;
    }

    var statsMsgs = ['强烈推荐安卓+微信下加速播放，苹果无加速机制！','扫描页面中的二维码，影视机器人为您找电影！'];

    if(vod_name){
        document.title=vod_name+'_'+vod_id+'_'+play_sid+'_'+play_pid;
    }


    var webdata = {
        set:function(key,val){
            window.sessionStorage.setItem(key,val);
            window.localStorage.setItem(key,val);
        },
        get:function(key){
            var val= window.sessionStorage.getItem(key);
            if(!val){
                val=window.localStorage.getItem(key);
            }
            return val;
        },
        del:function(key){
            window.sessionStorage.removeItem(key);
            window.localStorage.removeItem(key);
        },
        clear:function(key){
            window.sessionStorage.clear();
        }
    };

    // 是否自动播放，当自动进入电视剧的下一集的时候为true
    autoplay = false;
    if(vod_cid!=1 && webdata.get('autoplay_'+vod_id)){
        webdata.del('autoplay_'+vod_id);
        autoplay=true;
    }
    var dp = new DPlayer({
        container: document.getElementById('dplayer'),
        autoplay: autoplay,screenshot: true,preload:true,
        video: {
            pic:'/Public/ad/ad_300X250_1.png',
            url: m3u8url,
            type: 'customHls',
            customType: {
                'customHls': function (video, player) {
                    const hls = new Hls({
                        debug: false,
                        // Other hlsjsConfig options provided by hls.js
                        maxBufferLength:360,
                        memoryCacheLimit: 1024*1024*200,
                        diskCacheLimit:1024 * 1024 * 2024,
                        p2pConfig: {
                            logLevel: 'warn',
                            live: false,        // 如果是直播设为true
                        }
                    });
                    hls.loadSource(video.src);
                    hls.attachMedia(video);
                    hls.p2pEngine.on('stats', function (stats) {
                        _totalHTTPDownloaded = stats.totalHTTPDownloaded;
                        _totalP2PDownloaded = stats.totalP2PDownloaded;
                        _totalP2PUploaded = stats.totalP2PUploaded;

                    }).on('peerId', function (peerId) {
                        _peerId = peerId;
                    }).on('peers', function (peers) {
                        _peerNum = peers.length;
                        //updateStats();
                    });

                }
            }
        }
    });

    setTimeout(function () {
        var seekTime = webdata.get(seekKey);
        if(seekTime>10){
            dp.seek(seekTime);
        }
        // if(autoplay){
        //     dp.play();
        // }
    },300)

    setInterval(function(){
        updateStats();
        if(!playStatus){
            return;
        }
        webdata.set(seekKey,dp.video.currentTime);
    },1000);

    dp.on('playing', function () {

        if(!playStatus) {
            playStatus = true;
            var seekTime = webdata.get(seekKey);
            if (seekTime > 10) {
                dp.seek(seekTime);
            }
        }

        dealPlayRecords();
    });

    dp.on('ended', function () {
        playStatus=false;
        webdata.set(seekKey,0);
        // webdata.set('pay'+vod_id+'_'+play_pid , 0);
        if(vod_cid!=1) {
            webdata.set('autoplay_' + vod_id, 1);
        }
        window.parent.location.href=window.parent.cms_player.next_path;
    });

    dp.on('seeked', function () {
        hasSeeked=true;
    });

    function updateStats() {
        var t1 = new Date().getTime();
        t1=parseInt(t1/1000/10);
        var yushu = t1%( statsMsgs.length);
        if(Hls.isSupported() && yushu ==0 ){
            statsMsgs[0] =  'HTTP:' + (_totalHTTPDownloaded / 1024).toFixed(2) + 'MB P2P:' + (_totalP2PDownloaded / 1024).toFixed(2) + 'MB 节点' + _peerNum + '个';
        }

        var text = '';
        text = statsMsgs[yushu];
        document.querySelector('#stats').innerText = text
    }

    function dealPlayRecords(){

        // 更新播放记录
        var strRecords = webdata.get('playRecords');
        var records = [];
        if(strRecords ){
            records = JSON.parse(strRecords);
        }

        // 判断是否存在该电影 vod_id
        var _record=null;
        for(var i=0;i<records.length;i++ ){
            var record1 = records[i];
            if(record1['vod_id'] == vod_id  ){
                _record = record1;
                records.splice(i,1);
                break;
            }
        }

        if(vod_pic.indexOf('vod/')==0){
            vod_pic = '/Uploads/'+vod_pic;
        }
        // 如果_record == null，则新建
        if(!_record){
            _record={};
            _record['vod_id']=vod_id;
            _record['vod_name']=vod_name;

            if(records.length>=20){
                records.splice(19,records.length - 19);
            }
        }
        _record['vod_pic']=vod_pic;
        _record['play_sid']=play_sid;
        _record['play_pid']=play_pid;


        // 加到records 中的第一个
        records.splice(0,0,_record);
        webdata.set('playRecords',JSON.stringify(records));
    }
</script>

<?php } ?>
</body></html>